package realm.inorder.itmill.ui.panel.initial;

import org.openinsula.jazz.stereotype.Binding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import realm.inorder.i18n.Messages;
import realm.inorder.itmill.components.AbstractWindow;
import realm.inorder.itmill.controller.RegisterController;
import realm.inorder.itmill.session.SessionContext;
import realm.inorder.itmill.ui.window.MainWindow;
import realm.inorder.itmill.validator.EmailValidator;
import realm.inorder.persistence.model.Usuario;

import com.itmill.toolkit.data.validator.StringLengthValidator;
import com.itmill.toolkit.terminal.Sizeable;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.FormLayout;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.TextField;
import com.itmill.toolkit.ui.Button.ClickEvent;
import com.itmill.toolkit.ui.Button.ClickListener;

@Component
public class RegisterWindow extends AbstractWindow {

	@Binding(beanType = RegisterController.class, property = "usuario.nome")
	private TextField nomeField;

	@Binding(beanType = RegisterController.class, property = "usuario.login")
	private TextField loginField;

	@Binding(beanType = RegisterController.class, property = "usuario.senha")
	private TextField passwdField;

	@Binding(beanType = RegisterController.class, property = "usuario.email")
	private TextField emailField;

	private Button confirmButton, cancelButton;

	@Autowired
	private RegisterController registerController;
	
	@Autowired
	private MainWindow mainWindow;
	
	public RegisterWindow() {
		super(Messages.REGISTERWINDOW_TITLE.getMessage());
		setVisible(false);
		setWidth(300);
		setWidthUnits(Sizeable.UNITS_PIXELS);
		setHeight(190);
		setHeightUnits(Sizeable.UNITS_PIXELS);
		setPositionX(181);
		setPositionY(180);
	}

	public void initComponents() {
		FormLayout formLayout = new FormLayout();

		formLayout.addComponent(nomeField = new TextField(Messages.getBundleMessage("registerwindow.name")));
		formLayout.addComponent(loginField = new TextField(Messages.getBundleMessage("registerwindow.login")));
		formLayout.addComponent(passwdField = new TextField(Messages.getBundleMessage("registerwindow.passwd")));
		formLayout.addComponent(emailField = new TextField(Messages.getBundleMessage("registerwindow.email")));

		nomeField.addValidator(new StringLengthValidator(Messages.TEXTFIELD_LENGTH_VALIDATION.getMessage(Messages.getBundleMessage("registerwindow.name"), 1, 255), 1, 255, false));
		loginField.addValidator(new StringLengthValidator(Messages.TEXTFIELD_LENGTH_VALIDATION.getMessage(Messages.getBundleMessage("registerwindow.login"), 5, 16), 5, 16, false));
		passwdField.addValidator(new StringLengthValidator(Messages.TEXTFIELD_LENGTH_VALIDATION.getMessage(Messages.getBundleMessage("registerwindow.passwd"), 6, 16), 6, 16, false));
		emailField.addValidator(new StringLengthValidator(Messages.TEXTFIELD_LENGTH_VALIDATION.getMessage(Messages.getBundleMessage("registerwindow.email"), 4, 255), 4, 255, false));
		emailField.addValidator(new EmailValidator(Messages.EMAIL_VALIDATION.getMessage()));
		
		passwdField.setSecret(true);

		OrderedLayout buttonLayout = new OrderedLayout(OrderedLayout.ORIENTATION_HORIZONTAL);

		buttonLayout.addComponent(confirmButton = new Button(Messages.getBundleMessage("registerwindow.confirm")));
		buttonLayout.addComponent(cancelButton = new Button(Messages.getBundleMessage("registerwindow.cancel")));

		buttonLayout.setSpacing(true);
		buttonLayout.setMargin(false);

		formLayout.addComponent(buttonLayout);

		formLayout.setSpacing(true);
		formLayout.setMargin(true);

		this.addComponent(formLayout);
	}

	@Override
	public void clearComponents() {
		nomeField.setValue("");
		loginField.setValue("");
		passwdField.setValue("");
		emailField.setValue("");
	}

	public void validateComponents() throws Exception {
		nomeField.validate();
		loginField.validate();
		passwdField.validate();
		emailField.validate();
	}

	public void initActions() {
		confirmButton.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				if (validateForm()) {
					Usuario usuario = registerController.saveUsuario();
					SessionContext.put(Usuario.class, usuario);
					RegisterWindow.this.setVisible(false);
					mainWindow.showLoggedLayout();
				}
			}
		});

		cancelButton.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				clearComponents();
				RegisterWindow.this.setVisible(false);
			}
		});
	}

}
